package com.longcai.service.impl.property;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longcai.bean.PageHelper;
import com.longcai.entity.customer.ClyjUserProperty;
import com.longcai.entity.property.ClyjStatement;
import com.longcai.entity.property.ClyjUserPropertyRecord;
import com.longcai.mapper.property.ClyjUserPropertyMapper;
import com.longcai.mapper.property.ClyjUserPropertyRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.property.ClyjUserPropertyRecordService;
import com.longcai.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 用户资产收益记录表 服务实现类
 * </p>
 *
 * @author kh
 * @since 2019-04-29
 */
@Service
public class ClyjUserPropertyRecordServiceImpl extends ServiceImpl<ClyjUserPropertyRecordMapper, ClyjUserPropertyRecord> implements ClyjUserPropertyRecordService {


    @Autowired
    private ClyjUserPropertyRecordMapper clyjUserPropertyRecordMapper;
    @Autowired
    private ClyjUserPropertyMapper clyjUserPropertyMapper;

    /**
     *  查询明细列表 分页
     * @param userPropertyRecord
     * @param pageHelper
     * @return
     */
    public PageTableResponse getUserPropertyRecords(ClyjUserPropertyRecord userPropertyRecord, PageHelper pageHelper){
        //查询余额明细  分页
        Page<ClyjUserPropertyRecord> page = new Page<ClyjUserPropertyRecord>(pageHelper.getStart(), pageHelper.getLength());
        QueryWrapper<ClyjUserPropertyRecord> recordQueryWrapper = new QueryWrapper();
        recordQueryWrapper.eq("user_id",userPropertyRecord.getUserId());
        recordQueryWrapper.eq("flag",userPropertyRecord.getFlag());
        recordQueryWrapper.orderByDesc("create_time");

        //查询条件不为空添加查询条件
        if(userPropertyRecord.getTradeNo() != null && !userPropertyRecord.getTradeNo().trim().equals("")){
            recordQueryWrapper.eq("trade_no",userPropertyRecord.getTradeNo());
        }
        if(userPropertyRecord.getState() != null && !userPropertyRecord.getState().trim().equals("")){
            recordQueryWrapper.eq("state",userPropertyRecord.getState());
        }
        if(userPropertyRecord.getStartTime() != null && !userPropertyRecord.getStartTime().trim().equals("")){
            recordQueryWrapper.gt("create_time",userPropertyRecord.getStartTime());
        }
        if(userPropertyRecord.getEndTime() != null && !userPropertyRecord.getEndTime().trim().equals("")){
            recordQueryWrapper.le("create_time",userPropertyRecord.getEndTime());
        }

        IPage<ClyjUserPropertyRecord> recordIPage = this.clyjUserPropertyRecordMapper.selectPage(page, recordQueryWrapper);
        PageTableResponse pageTableResponse = new PageTableResponse(recordIPage.getTotal(),
                recordIPage.getTotal(), recordIPage.getRecords());

        return pageTableResponse;
    }

    @Override
    public PageTableResponse selectShopPropertyStatements(String userId, String type, PageHelper pageHelper) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        if(pageHelper.getLength() != null){
            map.put("start",pageHelper.getStart()<1?0:pageHelper.getStart()-1);
            map.put("length",pageHelper.getLength());
        }else  map.put("start",null);

        map.put("type",(type.equals("2")?"%Y-%m":"%Y-%m-%d"));

        //查询账户余额
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        ClyjUserProperty clyjUserProperty = clyjUserPropertyMapper.selectOne(queryWrapper);
        //查询总条数
        int counts = this.clyjUserPropertyRecordMapper.selectShopPropertyStatementsCounts(map);

        //合并日期相同数据
        List<ClyjStatement> clyjUserPropertyRecordList = this.clyjUserPropertyRecordMapper.selectShopPropertyStatements(map);
        Iterator<ClyjStatement> it = clyjUserPropertyRecordList.iterator();
        while (it.hasNext()){
            ClyjStatement clyjStatement = it.next();
            Iterator<ClyjStatement> its = clyjUserPropertyRecordList.iterator();
            while (its.hasNext()){
                ClyjStatement cs = its.next();
                cs.setAccout(clyjUserProperty.getAccount());
                if(cs.getExpenses().equals("0")){
                    cs.setIncome(cs.getValue());
                    cs.setExpend(new BigDecimal(0));
                }else {
                    cs.setIncome(new BigDecimal(0));
                    cs.setExpend(cs.getValue());
                }
                if(cs.getTime().equals(clyjStatement.getTime()) && !cs.getExpenses().equals(clyjStatement.getExpenses())){
                    if(clyjStatement.getExpenses().equals("0")){
                        cs.setIncome(clyjStatement.getValue());
                    }else {
                        cs.setExpend(clyjStatement.getValue());
                    }
                    it.remove();
                    break;
                }
            }
        }
        PageTableResponse pageTableResponse = new PageTableResponse(counts,counts, clyjUserPropertyRecordList);
        return pageTableResponse;
    }
}
